﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using MySql.Data.MySqlClient;
using 包覆门.AccountManager;
namespace 包覆门
{
    public partial class accountForm : Form
    {
        private DataTable dtAcc = new DataTable();
        private AccountDAL dal = new AccountDAL();
       // private int flag = 0;//1:新增 2:更新
        public accountForm()
        {
            InitializeComponent();
            dgAccount.AutoGenerateColumns = false;
        }

        #region 动作事件
        //窗口加载
        private void accountForm_Load(object sender, EventArgs e)
        {
            Dictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(0,"大唐宅配账号");
            dic.Add(1, "外包公司账号");
            BindingSource bs = new BindingSource();
            bs.DataSource = dic;
            this.company.DataSource = bs;
            this.company.DisplayMember = "value";
            this.company.ValueMember = "key";
            LoadData();
            //SetState(true, true, true, false, true);
            
        }
        //关闭过程中，将af对象清空
        private void accountForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            mainform.af = null;
        }
        //新增按钮
        private void tsbAdd_Click(object sender, EventArgs e)
        {
            AccountEditing ae = new AccountEditing();
            ae.IsNew = true;
            if(ae.ShowDialog()==DialogResult.OK){
                LoadData();
            
            }
           // ((DataTable)dgAccount.DataSource).Rows.Add();
           // SetState(true, false, true, true, false);
           // flag = 1;


        }
        //保存
        //private void tsbSave_Click(object sender, EventArgs e)
        //{
        //    dgAccount.CurrentCell = null;

        //    DataTable saveData = dtAcc.GetChanges();
        //    if(saveData.Rows.Count>0){
        //        try
        //        {
        //            if (dal.SaveData(saveData) > 0)
        //            {
        //                MessageBox.Show("保存成功");
        //            }
        //            else {

        //                MessageBox.Show("保存失败");
        //            }
        //        }
        //        catch (MySqlException ex)
        //        {

        //            MessageBox.Show(ex.Message);
        //        }
        //    }else{
        //         MessageBox.Show("暂没有要保存的数据");
        //    }
        //    //flag = 0;
        //    //SetState(true, true, true, false, true);
        //}

        //删除
        private void tsbDel_Click(object sender, EventArgs e)
        {
            int count = dgAccount.SelectedRows.Count;
            if (count < 0)
            {
                MessageBox.Show("请选中要删除的行");
                return;
            }
            else if (count > 1)
            {
                MessageBox.Show("不可多选");
                return;
            }
            else
            {
                if (DialogResult.Yes == MessageBox.Show("确认要删除", "提示框", MessageBoxButtons.YesNo, MessageBoxIcon.Information))
                {
                    string ai = dgAccount.Rows[this.dgAccount.CurrentCell.RowIndex].Cells["aid"].Value.ToString();
                    int aid = int.Parse(ai);
                    int delcount = dal.DeleData(aid);
                    if (delcount == 1)
                    {
                        MessageBox.Show("删除成功");
                        LoadData();
                    }
                    else {
                        MessageBox.Show("删除失败");
                    }
                    
                    //SetState(true, true, true, false, true);
                }
                
                
            }
            
            
        }
        //关闭
        private void tsbClose_Click(object sender, EventArgs e)
        {
            Close();
        }
        //编辑
        private void tsbEdit_Click(object sender, EventArgs e)
        {
           // SetState(false, false, true, true, true);
            int count = this.dgAccount.SelectedRows.Count;
            if (count <= 0) {
                MessageBox.Show("请选择要删除的行");
                return;
            }
            string aid = this.dgAccount.SelectedRows[0].Cells["aid"].Value.ToString();
            AccountEditing ae = new AccountEditing();
            ae.EditingID = aid;
            ae.IsNew = false;
            if(ae.ShowDialog()==DialogResult.OK){
                LoadData();
            }

        }


        #endregion 动作事件

        #region  私有方法
        //加载银行账户信息
        private void LoadData() {
            string acname = this.tbname.Text.Trim();
            string acbank = this.tbbank.Text.Trim();
            string account = this.tbaccount.Text.Trim();
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("acname", acname);
            dic.Add("acbank", acbank);
            dic.Add("account",account);

            dtAcc = dal.ListAll(dic);
            dgAccount.DataSource = null;
            dgAccount.DataSource=dtAcc;
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            LoadData();
        }
        //状态机
        //private void SetState(bool add,bool edit,bool del,bool save,bool close) {
        //    this.tsbAdd.Enabled = add;
        //    this.tsbDel.Enabled = del;
        //    this.tsbEdit.Enabled = edit;
        //   // this.tsbSave.Enabled = save;
        //    this.tsbClose.Enabled = close;
        //}
        #endregion  私有方法

        
        


       

    }
}
